package com.picture.wallpaper.service.impl;




import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.picture.wallpaper.core.commons.Result;
import com.picture.wallpaper.core.utils.AES;
import com.picture.wallpaper.core.utils.TypeUtils;
import com.picture.wallpaper.dao.dto.ResultDto;
import com.picture.wallpaper.dao.entity.PictureList;
import com.picture.wallpaper.dao.entity.UserGetPicture;
import com.picture.wallpaper.dao.entity.UserLoginWx;
import com.picture.wallpaper.dao.mapper.pictureListMapper;
import com.picture.wallpaper.dao.mapper.userGetPictureMapper;
import com.picture.wallpaper.dao.mapper.userLoginWxMapper;
import com.picture.wallpaper.dao.vo.EncipherPictureDownloadVo;
import com.picture.wallpaper.manager.OBS.ObsUtils;
import com.picture.wallpaper.manager.check.UserShiroCheck;
import com.picture.wallpaper.manager.redis.RedisUtil;
import com.picture.wallpaper.service.pictureService;
import io.swagger.annotations.ApiModelProperty;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import com.picture.wallpaper.dao.dto.PictureDownloadDto;

import javax.annotation.Resource;
import java.util.Objects;

import static com.picture.wallpaper.core.constant.ObsConstant.OBS_BUCKET_NAME;
import static com.picture.wallpaper.core.constant.RedisConstant.REDIS_CHECK;
import static com.picture.wallpaper.core.constant.RedisConstant.REDIS_PICTURE_URL;

@Service
@Slf4j
public class pictureServiceImpl implements pictureService {
    @Resource
    private AES aes;
    @Resource
    private PictureDownloadDto pictureDownLoadDto;
    @Resource
    private TypeUtils typeUtils;
    @Resource
    private ObsUtils obsUtils;
    @Resource
    private EncipherPictureDownloadVo encipherPictureDownloadVo;
    @Resource
    private UserGetPicture userGetPicture;
    @Resource
    private userGetPictureMapper userGetPictureMapper;
    @Resource
    private pictureListMapper picturelistMapper;
    @Resource
    private UserShiroCheck userShiroCheck;
    @Resource
    private UserLoginWx userLoginWx;
    @Resource
    private PictureList pictureList;
    @Resource
    private userLoginWxMapper userLoginWxMapper;
    @Resource
    private RedisUtil redisUtil;
//    @Resource
//    private ResultDto resultDto;


    @Override
    @ApiModelProperty("用户效验")
    public ResultDto checkUser(EncipherPictureDownloadVo encipherPictureDownloadVo) {
        pictureDownLoadDto=typeUtils.typeConversion(encipherPictureDownloadVo);
        String openId=aes.decrypt(encipherPictureDownloadVo.getEncipherOpenId());
        LambdaQueryWrapper<UserLoginWx> loginWxLambdaQueryWrapper=new LambdaQueryWrapper<>();
        loginWxLambdaQueryWrapper.eq(UserLoginWx::getOpenId,openId).eq(UserLoginWx::getUserShiro,true);
        userLoginWx=userLoginWxMapper.selectOne(loginWxLambdaQueryWrapper);
        if (Objects.isNull(userLoginWx)){
            return ResultDto.fail("不存在该用户","400");
        }
        boolean a=redisUtil.hset(REDIS_CHECK,openId,pictureDownLoadDto,3600L);
        if (a){
            return ResultDto.success(openId,"999");
        }else {
            return ResultDto.fail("缓存存入失败","101");
        }
    }

    @ApiModelProperty("下载图片临时链接")
    @Override
    public Result getPictureUrl (String openId)  {
        Object object= redisUtil.hget(REDIS_CHECK,openId);
        pictureDownLoadDto=typeUtils.objectToPictureDownloadDto(object);
        Object ob=redisUtil.hget(REDIS_PICTURE_URL,pictureDownLoadDto.getPictureName());
        if (!Objects.isNull(ob)){
            pictureDownLoadDto=typeUtils.objectToPictureDownloadDto(ob);
            return Result.success(pictureDownLoadDto);
        }
        /*再写redis查不到之后的操作*/
//        /*图片资源redis查询*/
//        boolean redisCheck=redisUtil.hHasKey(REDIS_PICTURE_URL,pictureDownLoadDto.getPictureName());
//        if (redisCheck){
//            Object object= redisUtil.hget(REDIS_PICTURE_URL,pictureDownLoadDto.getPictureName());//如果存在就通过key获取对象
//            pictureDownLoadDto=typeUtils.objectToPictureDownloadDto(object);//object转换为对象
//            return Result.success(pictureDownLoadDto.getPictureUrl());
//        }
//        /* 图片资源数据库查询*/
//        pictureList=userShiroCheck.pictureCheck(pictureDownLoadDto.getPictureName());
//        if (Objects.isNull(pictureList)){
//            return Result.fail(2,"w");
        }
//        /*
//        *图片地址查询并存入redis
//        */
//        String objectKey=pictureDownLoadDto.getPictureNumber()+"/"+pictureDownLoadDto.getPictureName();
//        String pictureUrl=obsUtils.GetObjectUrl(3600L,OBS_BUCKET_NAME,objectKey);
//        pictureDownLoadDto.setPictureName(pictureList.getPictureName());
//        pictureDownLoadDto.setPictureId(pictureList.getPictureId());
//        pictureDownLoadDto.setPictureNumber(pictureList.getPictureNumber());
//        pictureDownLoadDto.setPictureUrl(pictureUrl);
//        boolean mysqlInsert=true;
//        userGetPicture.setPictureId(pictureDownLoadDto.getPictureId());
//        userGetPicture.setOpenId(pictureDownLoadDto.getOpenId());
//        if (Objects.isNull(pictureUrl)){
//            mysqlInsert=false;
//            userGetPicture.setResult(mysqlInsert);
//            userGetPictureMapper.insert(userGetPicture);
//            return Result.fail("OBS请求失败");
//        }
//        boolean redisInsert= redisUtils.hset(pictureDownLoadDto.getPictureNumber(),pictureDownLoadDto.getPictureName(),pictureDownLoadDto,3000L);
//        /*
//        *存取用户操作到数据库
//        */
//        userGetPictureMapper.insert(userGetPicture);
//        return Result.success(aes.encrypt(pictureDownLoadDto.getPictureUrl()));
        return null;
    }



}
